package org.example.service;


import org.example.mapper.StudentMapper;
import org.example.mapper.ExperimentMapper;
import org.example.model.ExperimentStats;
import org.example.model.Student;
import org.example.model.Experiment;
import org.example.model.StudentStats;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class StatService {
    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private ExperimentMapper experimentMapper;

    // 学生维度统计：生成每位学生缺交的实验编号列表
    public Map<Student, StudentStats> getStudentStats() {
        Map<Student, StudentStats> studentStatsMap = new HashMap<>();

        List<Student> students = studentMapper.getAllStudents();
        List<Experiment> experiments = experimentMapper.getAllExperiments();

        for (Student student : students) {
            StudentStats stats = new StudentStats();
            stats.setName(student.getName());
            stats.setStudentId(student.getId());

            List<String> missingExperiments = new ArrayList<>();
            int missingCount = 0;

            for (Experiment experiment : experiments) {
                // 模拟检查学生是否提交实验的逻辑
                // 实际项目中，这里应该根据你的数据库设计进行查询
                boolean isSubmitted = false; // 默认为未提交

                if (!isSubmitted) {
                    missingExperiments.add(experiment.getName());
                    missingCount++;
                }
            }

            stats.setMissingExperiments(String.join("、", missingExperiments));
            stats.setMissingCount(missingCount);
            studentStatsMap.put(student, stats);
        }

        return studentStatsMap;
    }

    // 实验维度统计：生成每个实验的缺交学生名单
    public Map<Experiment, ExperimentStats> getExperimentStats() {
        Map<Experiment, ExperimentStats> experimentStatsMap = new HashMap<>();

        List<Experiment> experiments = experimentMapper.getAllExperiments();
        List<Student> students = studentMapper.getAllStudents();

        for (Experiment experiment : experiments) {
            ExperimentStats stats = new ExperimentStats();
            stats.setExperimentName(experiment.getName());
            stats.setTotalStudents(students.size());

            List<Student> missingStudents = new ArrayList<>();
            int missingCount = 0;

            for (Student student : students) {
                // 模拟检查学生是否提交实验的逻辑
                // 实际项目中，这里应该根据你的数据库设计进行查询
                boolean isSubmitted = false; // 默认为未提交

                if (!isSubmitted) {
                    missingStudents.add(student);
                    missingCount++;
                }
            }

            stats.setMissingStudents(missingStudents);
            stats.setMissingCount(missingCount);
            stats.setSubmissionRate(calculateSubmissionRate(stats.getTotalStudents(), stats.getMissingCount()));
            experimentStatsMap.put(experiment, stats);
        }

        return experimentStatsMap;
    }

    // 计算提交率
    private double calculateSubmissionRate(int totalStudents, int missingCount) {
        if (totalStudents == 0) {
            return 0.0;
        }
        return ((double) (totalStudents - missingCount) / totalStudents) * 100;
    }
}

